"use strict";
var express = require('express');
var router = express.Router();

var App = require("../model/AppModel").App;



router.post('/PostAppModel', function (req, res, next) {
    var json =
        {
            "code": "1",
            "succeed": "succeed",
            "content": {
                "msg": "密码错误"
            }
        };
    var platform = req.param('platform');
    var app_ame = req.param('app_ame');
    var app_path = req.param('down_path');
    var app_type = req.param('app_type');
    var img = req.param('img');
    var username = req.param('username');
    var version_des = req.param('version_des');

    if (0==platform .length) {
        FailureJson(json, res, "没有传平台参数");
        return;
    } else if (0== app_ame.length) {
        FailureJson(json, res, "微应用名字为空");
        return;
    } else if (0 == img.length) {
        FailureJson(json, res, "微应用logo为空");
        return;
    } else if (0 == version_des.length) {
        FailureJson(json, res, "微应用介绍为空");
        return;
    }
    App.find({'app_ame': app_ame,'username':username}, function (err, docs) {
        // docs 是查询的结果数组 });
        if (err) {
            console.log(err);
        } else if (docs.length>0) {
            FailureJson(json, res, "微应用名字已经存在");
        }else {
            //可以注册
            var app = new App({
                app_ame: app_ame,
                app_type: app_type,
                down_path: app_path,
                img: img,
                version_des: version_des,
                username: username,
            });
            app.save(function (err) {
                if (err) {
                    console.log(err);
                } else {
                    console.log('存入成功');
                    SucceeJson(json, res, "存入成功");
                }
            });
        }

    }).limit(10).sort('-create_date');



});

router.get('/LoadAllApp',function (req, res, next) {
    var json =
        {
            "code": "1",
            "succeed": "succeed",
            "content": {
                "msg": []
            }
        };
   var  username = req.param('username');
   var  app_ame = req.param('app_ame');
    var search;
    if(typeof(username)!="undefined")
         search = {'username': username};
    else
         search = {'app_ame': app_ame};
    App.find(search,null,{limit: 10},function (err, posts) {
        var msg_arr=json.content.msg;
        for (let i=0;i<posts.length;i++)
        {
            msg_arr[i]=posts[i];
        }
        json.code = "1";
        json.succeed = "succeed";
        res.send(json);
    }).sort('-create_date');

});

router.get('/deleteApp', function (req, res) {
    var json= {
        "code": "1",
        "succeed": "succeed",
        "content": {
            "msg": ""
        }
    };
    var app_ame = req.param('app_ame');
    var username = req.param('username');
    var search = {'app_ame': app_ame,'username':username};
    App.remove(search,function (err, posts) {
        if (err) {
            throw err;
        }
        else {
            json.code = "1";
            json.succeed = "succeed";
            json.content.msg="成功删除";
            res.send(json);
        }
    });
});
var FailureJson = function (json, res, msg) {
    json.code = "0";
    json.succeed = "failure";
    json.content.msg = msg;
    res.send(json);
};
var SucceeJson = function (json, res, msg) {
    json.code = "1";
    json.succeed = "succeed";
    json.content.msg=msg;
    res.send(json);
};

module.exports = router;